<template>
  <div style="margin:20px">
    <!-- template 内部用来写页面的内容 -->
    <!-- style: 用来写样式 -->
    <!-- script:用来写js脚本 -->
    <el-form label-width="80px">
      <el-form-item label="讲师姓名">
        <el-input v-model="teacher.name"/>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro"/>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level" >
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师头像">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="uploadPath"
          class="avatar-uploader">
          <!-- img标签用来回显上传成功的图片 -->
          <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>

      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort" :min="0" />
      </el-form-item>

      <el-form-item label="入驻时间">

        <el-date-picker v-model="teacher.joinDate" type="date" value-format="yyyy-MM-dd"/>

      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">提交</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<style>

</style>
<script>
import teacherApi from '@/api/teacher'
export default { // 内部和new Vue({  })
  data() {
    return {
      uploadPath: 'http://localhost:8120/admin/oss/file/upload?module=avatar', // 上传图片的接口地址
      teacher: {
        level: 1, // 默认是1，需在绑定的地方改为:value  前加：
        sort: 0
      }
    }
  },
  created() {
    // 获取路径中id的参数
    // $router  代表vue绑定的整个路由配置
    // $route  代表 访问当前页面的路由对象
    console.log('id:' + this.$route.params.id)
    // 如果是更新才查询数据回显
    if (this.$route.params.id) {
      this.getTeacherById(this.$route.params.id)
    }
  },
  methods: {
    // 图片上传成功的回调方法
    handleAvatarSuccess(res, file) { // res代表响应对象  file代表本地上传的文件
      // this.imageUrl = URL.createObjectURL(file.raw)
      this.teacher.avatar = res.data.path // 获取上传成功的图片地址  设置给teacher对象
      // 让vue强制渲染
      this.$forceUpdate()
    },
    // 图片上传之前的回调
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 编辑讲师，先查询
    getTeacherById(id) {
      teacherApi.getById(id).then(r => {
        this.teacher = r.data.item
      })
    },

    // 清空
    reset() {
      this.teacher = {}
    },
    saveOrUpdate() {
      if (this.teacher.id) {
        // 更新
        this.update()
      } else {
        // 新增
        this.save()
      }
    },
    update() {
      teacherApi.update(this.teacher).then(r => {
        this.$message.success('更新讲师成功')
        this.$router.push('/teacher/list')
      })
    },
    save() {
      // 提交新增请求
      teacherApi.save(this.teacher).then(r => {
        this.$message.success('新增讲师成功')
        // 跳转到讲师列表页面
        // 以路由的方式加载路由对应的讲师列表页面  局部刷新
        this.$router.push('/teacher/list')// 根据vue对象的路由配置 置顶路由地址 进行路由
      })
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
